<?php


namespace App\Providers;

use App\Models\Settings;
use Illuminate\Support\ServiceProvider;

class UploadConfigServiceProvider extends ServiceProvider
{
    public function boot()
    {
        $config = Settings::where('group', '=', 'upload')->get();
        if ($config->count()) {
            $config = $config->pluck('value', 'name')->all();

            $driver = $config['driver'] == 'local' ? 'local' : 'oss';

            $this->app->get('config')->set('filesystems.default', $driver);

            $this->app->get('config')->set('filesystems.upload_driver', $config['driver']);

            // 目前只用阿里云oss
            if ($config['driver'] == 'ali_oss') {
                $this->app->get('config')->set('filesystems.disks.oss', [
                    'driver'     => 'oss',
                    'root'       => $config['ali_root'] ?? '',
                    'access_key' => $config['ali_access_key'],
                    'secret_key' => $config['ali_secret_key'],
                    'domain'     => $config['ali_oss_domain'] ?? '',
                    'endpoint'   => $config['ali_endpoint'],
                    'bucket'     => $config['ali_bucket'],
                    'isCName'    => false,
                ]);
            }
        }
    }

    public function register()
    {
    }
}
